package com.gitee.fantaros.web.fan.netcut.views;

import com.gitee.fantaros.web.fan.netcut.bean.PasterInfo;
import com.gitee.fantaros.web.fan.netcut.service.PasterService;
import com.gitee.fantaros.web.fan.netcut.util.Str;
import com.gitee.fantaros.web.fan.netcut.util.WhisperUtil;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentEvent;
import com.vaadin.flow.component.Key;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.PasswordField;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.*;
import com.vaadin.flow.spring.annotation.UIScope;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录页
 */
@Route("/paster_index")
@PageTitle("进入剪切板")
@UIScope
public class NetPasterView extends VerticalLayout implements BeforeEnterObserver {

    private final PasterService pasterService;

    private final TextField userName;

    private final PasswordField password;

    private final Div footLabel;

    public NetPasterView(@Autowired PasterService pasterService) {
        this.pasterService = pasterService;
        pasterService.prepareDatabaseTable();

        String loginWidth = "20%";
        String minWidth = "160px";

        Div head = new Div();
        head.setText("网络剪切板");
        head.setWidth(loginWidth);

        this.userName = new TextField();
        this.userName.setLabel("剪切板名");
        this.userName.setWidth(loginWidth);
        this.userName.setMinWidth(minWidth);

        this.password = new PasswordField();
        this.password.setLabel("密码");
        this.password.setWidth(loginWidth);
        this.password.setMinWidth(minWidth);
        this.password.addKeyPressListener(Key.ENTER, this::doLoginAction);

        Button loginButton = new Button();
        loginButton.setText("进入剪切板");
        loginButton.setWidth(loginWidth);
        loginButton.setMinWidth(minWidth);
        loginButton.addClickListener(this::doLoginAction);

        this.footLabel = new Div();
        this.footLabel.getStyle().setColor("red");

        add(head);
        add(userName);
        add(password);
        add(loginButton);
        add(footLabel);
        refreshLoginForm();
        setAlignItems(Alignment.CENTER);
    }

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        refreshLoginForm();
    }

    private <C extends Component> void doLoginAction(ComponentEvent<C> event) {
        String pasterName = userName.getValue();
        if (pasterName == null || pasterName.isEmpty()) {
            showWarn();
            return;
        }
        String passwd = password.getValue();
        if (passwd == null || passwd.isEmpty()) {
            showWarn();
            return;
        }
        String encodedPasswd = WhisperUtil.encodeString(passwd);
        PasterInfo pasterInfo = null;
        if (pasterService.checkExists(pasterName)) {
            //exists
            if (pasterService.checkEncodedPasswd(pasterName, encodedPasswd)) {
                pasterInfo = pasterService.queryInfoById(pasterName);
            }
        } else {
            pasterInfo = new PasterInfo();
            pasterInfo.setPasterName(pasterName);
            pasterInfo.setPasswd(encodedPasswd);
            pasterInfo.setCreateTime(Str.toString(new Date()));
            pasterService.insertOne(pasterInfo);
        }
        if (pasterInfo != null) {
            Map<String, String[]> params = new HashMap<>();
            params.put("name", new String[] { pasterName });
            params.put("pwd", new String[] { encodedPasswd });
            this.getUI().ifPresent(ui -> ui.navigate("content", QueryParameters.full(params)));
        } else {
            showWarn();
        }
    }

    private void showWarn() {
        footLabel.getStyle().setColor("red");
        footLabel.setText("请输入正确的剪切板名和密码");
    }

    private void refreshLoginForm() {
        footLabel.setText("");
        userName.setValue("");
        password.setValue("");
    }
}
